Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define methods on dynamic module #703

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

jbennett
Copy link

@jbennett jbennett commented Jul 3, 2024

Currently you cannot override the generated methods and access super ie:

class Transaction < ApplicationRecord
  monetize :price_cents

  def price=(value)
    # stuff
   super # NoMethodError
  end
end

This happens because the method is defined on the class directly so their is no super method to call. A solution today is to create an alias (alias :old_price= :price=) before overriding the method as is recommending in issue #507.

Instead, a new module can be created, included into the class, and the methods defined on it allowing super to work as expected.

Currently you cannot override the generated methods and access super ie:

```ruby
monetize :price_cents

def price=(value)
   # stuff
   super # NoMethodError
end
```

This happens because the method is defined on the class directly so their is no super method to call. A solution today is to create an alias (`alias :old_price= :price=`) before overriding the method as is recommending in issue RubyMoney#507.

Instead, a new module can be created, included into the class, and the methods defined on it allowing `super` to work as expected.
sunny
sunny previously approved these changes Jul 3, 2024
Copy link
Contributor

@sunny sunny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! Can you add an entry to the CHANGELOG as well?

sunny
sunny previously approved these changes Jul 4, 2024
Copy link
Contributor

@sunny sunny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests don’t seem to run though, but it doesn’t seem to be linked to your changes 🤔

@jbennett
Copy link
Author

jbennett commented Jul 4, 2024

Looks like #698 also had issues with sqlite3 and added a ~> 1.4 requirement to the main gemfile.

Copy link
Contributor

@sunny sunny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants